﻿<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
﻿<?php
  error_reporting(E_ALL^E_NOTICE^E_WARNING);
  ini_set('display_errors', '1');

  require(dirname(__FILE__).'/../classes/newloader.php');
  require(dirname(__FILE__).'/config.php'); //这里是关于接口路由选择的配置
  require(dirname(__FILE__).'/../classes/interface/rediscfg.php');  //这里是关于接口与redis等交互的配置

  require(dirname(__FILE__).'/allowlist.php');

  $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
  $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
  if (!in_array($user_IP,$allowList))
  {
	echo "Access denied.";
  	exit; //白名单控制
  }

//   if (!isset($_GET['phonenum']) || !isset($_GET['package']) || !isset($_GET['operator']) || !isset($_GET['keyid']))
//   {
//   	echo 'phonenum,package,operator is required!';
//   	exit;
//   }

 if(true) {
  $package  = $_GET['package']  ;
  $pphonenum =$_GET['phonenum'];
  $poperator =$_GET['operator'];
  $userid =   getUserid($_GET['keyid']) ;

  $mysql = "SELECT `renderRoute`('$userid' ,$pphonenum,'$package',$poperator) ; " ;

  $rec =  doQuery($mysql) ;
  $row = $rec->fetch_assoc ();
  $array = array_values($row);
  $_GET['operator']=($array[0]>0)?$array[0]:$_GET['operator'];

}else{
 if (strlen($_GET['package']) ==6  )  //6位是省份流量包
{
     $temp = $_GET['operator'];
	$userid =   getUserid($_GET['keyid']) ;
  $_GET['operator']=$user_setting[$userid][$_GET['package']]?$user_setting[$userid][$_GET['package']]:$user_setting['default'][$_GET['package']];
	if ($user_setting[$userid][$_GET['package']]) echo "【省份流量包】 匹配到 ：用户规定  规则！\r\n<br>" ;
	$_GET['operator']=$_GET['operator']?$_GET['operator']:$temp;

}else {
  $temp = $_GET['operator'];
  //默认配置
  $_GET['operator']= $route_setting[$_GET['operator']]?$route_setting[$_GET['operator']]:$_GET['operator'] ;


  //根据流量包
  $_GET['operator']=$route_setting[$_GET['package']]?$route_setting[$_GET['package']]:$_GET['operator'];


 //根据运营商省份确定通道，如 route_setting['中国移动']['山西'] 找到对应的通道号。
 // $phoneinfo =  getPhoneNumInfo($_GET['phonenum']) ;
 // $_GET['operator']=$route_setting[$phoneinfo['corp']][$phoneinfo['province_code']]?$route_setting[$phoneinfo['corp']][$phoneinfo['province_code']]:$_GET['operator'];

  //根据用户配置
  $userid =   getUserid($_GET['keyid']) ;
  $_GET['operator']=$user_setting[$userid][$temp]?$user_setting[$userid][$temp]:$_GET['operator'];

  $_GET['operator']=$_GET['operator']?$_GET['operator']:$temp;

}
}

  $flowclass=$flow_setting[$_GET['operator']]['interface'];
  if ($flowclass=='ICMCCPackage')
    $tunnelid=1;
  elseif ($flowclass=='ICUCCPackage')
    $tunnelid=2;
  elseif ($flowclass=='ICTCCPackage')
    $tunnelid=3;
  elseif ($flowclass=='IDHSTPackage')
    $tunnelid=4;
  elseif ($flowclass=='IAliFlowWalletPackage')
    $tunnelid=5;
  elseif ($flowclass=='IYGPackage')
    $tunnelid=6;
  elseif ($flowclass=='IETUOPackage')
    $tunnelid=7;
  elseif ($flowclass=='IHWPackage')
    $tunnelid=8;





   $flow=new $flowclass(array('action'=>'charge','operid'=>$_GET['operator'],'phonenum'=>$_GET['phonenum'],'package'=>$_GET['package'],'orderid'=>$_GET['keyid'],'tunnelid'=>$tunnelid));  //tunnelid可以通过动态路由来选择
   $flow->logf("info","Single_charge(dai) : operid=".$_GET['operator'].',phonenum='.$_GET['phonenum'].',package='.$_GET['package'].',orderid='.$_GET['keyid'].',tunnelid='.$tunnelid);
   $result=$flow->charging();
   $flow->logf("info","Single_charge : operid=".$_GET['operator'].',phonenum='.$_GET['phonenum'].',package='.$_GET['package'].',orderid='.$_GET['keyid'].',tunnelid='.$tunnelid);
  //$flow=new $flow_setting['1']['interface'](array('action'=>'query','operator_id'=>'1','operseq'=>'YG20150917160452415','orderid'=>'1234_15801488221','starttime'=>'201509170000','endtime'=>'201509172400'));
   //$result=$flow->query();

  echo $result;


function doQuery($str) {
   require(dirname ( __FILE__ ) . '/../data/config3.php');
    // $connection = mysqli_connect ( $db_host, $db_user, $db_pass, $db_name ) or die ( "Unable to connect!" );
    $connection = mysqli_connect ( explode(":",$db_host)[0], $db_user, $db_pass, $db_name,explode(":",$db_host)[1] )or die ( "Unable to connect!" );

    mysqli_query($connection,'set names utf8');
    $result = mysqli_query ( $connection, $str ) or die ( "Error in query. " . mysql_error () );

    return $result;
  }


  function getPhoneNumInfo($pureNum) {
    $phoneNumHead = substr ( $pureNum, 0, 7 );
    $rec = doQuery ( "SELECT province ,   corp  FROM  uni_mobile_belong a  WHERE    mobile =  \"" . $phoneNumHead . "\"" );
    if ($rec == false)
      $ret = array (
            'corp' => '00',
            'province_code' =>'00'
      );
    else {
      if ($rec->num_rows <= 0) {
        $ret = array (
            'corp' => '00',
            'province_code' =>'00'
        );
      } else {

        $row = $rec->fetch_assoc ();
        $ret = array (
            'corp' => $row['corp'],
            'province_code' => $row ['province']
        );

      }
    }
    return $ret;

  }


  function getUserid ($ordersn) {

    require(dirname(__FILE__).'/../data/config3.php');
    $redis=new Redis();
    if (isset($redis_host))
    {
      $result=$redis->connect($redis_host,$redis_port);
      $redis->select($redis_db);
    }
    else
    {
      $result=$redis->connect($GLOBALS['redis_host'],$GLOBALS['redis_port']);
      $redis->select($GLOBALS['redis_db']);
    }

    return   $redis->hget($ordersn ,'userid');

  }
